package action;

import bean.Project;
import bean.Worker;
import com.opensymphony.xwork2.Action;

import dao.ProjectDAO;
import dao.ProjectDAOImpl;
import dao.WorkerDAO;
import dao.WorkerDAOImpl;
import tool.HibernateSessionFactory;

import java.util.List;

public class WorkerAction implements Action {
    private ProjectDAO projectDAO = new ProjectDAOImpl(HibernateSessionFactory.getSession());
    private List<Project> projects;
    private WorkerDAO workerDAO = new WorkerDAOImpl(HibernateSessionFactory.getSession());
    private Worker worker;
    private List<Worker> workers;

    public List<Project> getProjects() {
        return projects;
    }

    public Worker getWorker() {
        return worker;
    }

    public void setWorker(Worker worker) {
        this.worker = worker;
    }

    public List<Worker> getWorkers() {
        return workers;
    }

    @Override
    public String execute() throws Exception {
        projects = projectDAO.queryAll();
        workers = workerDAO.queryAll();
        return SUCCESS;
    }

    public String add() {
        return workerDAO.add(worker) ? "add" : null;
    }

    public String edit() {
        projects = projectDAO.queryAll();
        workers = workerDAO.queryByWorkerId(worker.getWorkerId());
        worker = workers.get(0);
        return "edit";
    }

    public String modify() {
        return workerDAO.edit(worker) ? "modify" : null;
    }

    public String remove() {
        return workerDAO.remove(worker.getWorkerId()) ? "remove" : null;
    }

    public String queryByName() {
        workers = workerDAO.queryByWorkerName(worker.getWorkerName());
        return "queryByName";
    }

    public String queryByTelephone() {
        workers = workerDAO.queryByTelephone(worker.getTelephone());
        return "queryByTelephone";
    }

    public String queryById() {
        workers = workerDAO.queryByWorkerId(worker.getWorkerId());
        return "queryById";
    }
}
